﻿#include <stdio.h>
#include <string>

#if defined(XP_UNIX)
#include <uuid/uuid.h>

typedef struct _GUID
{
    unsigned long   Data1;
    unsigned short  Data2;
    unsigned short  Data3;
    unsigned char   Data4[8];
} GUID, UUID;
#else
#ifndef snprintf
#define snprintf		_snprintf
#endif
#include <objbase.h>
#endif

std::string GetUUID()
{
    GUID            guid        = {0};
    std::string     str;

#if defined(XP_UNIX)
    uuid_generate( reinterpret_cast<unsigned char*>(&guid) );
#else
    CoCreateGuid(&guid);
#endif
    str.resize(64, 0);
	snprintf((char *)str.c_str(), str.size()
             , "{%08lx-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x}"
             , guid.Data1, guid.Data2, guid.Data3
             , guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3]
             , guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
    return str;
}

